from flask import Flask
from flask_cors import CORS
from dotenv import load_dotenv
import os
import logging

# 加载环境变量
load_dotenv('.flaskenv')

# 配置日志级别
logging.getLogger("paddleocr").setLevel(logging.WARNING)

# 创建应用


def create_app():
    app = Flask(__name__)
    CORS(app)

    # 加载配置
    from config import configure_app
    configure_app(app)

    # 注册模块
    from modules.ocr.controller import ocr_blueprint
    app.register_blueprint(ocr_blueprint)

    # 注册模板匹配模块
    from modules.template_match.controller import template_match_blueprint
    app.register_blueprint(template_match_blueprint)

    # 注册图像相似度比较模块
    from modules.image_similarity.controller import image_similarity_blueprint
    app.register_blueprint(image_similarity_blueprint)

    return app


app = create_app()

if __name__ == "__main__":
    debug_mode = os.environ.get("FLASK_DEBUG", "0") == "1"
    port = int(os.environ.get("FLASK_RUN_PORT", 5001))
    print(
        f"Starting server in {'debug' if debug_mode else 'production'} mode...")
    app.run(host="0.0.0.0", port=port, debug=debug_mode)
